//import other javascript - jquery framework
$import("resource/js/ysframework.js");
$import("resource/css/treeview.css");

var ystreecheckboxname = "ystreeCheckBoxName";
var valueContainerId = "nodeid";//存放checkbox值的input控件的ID,配合你页面中定义的input控件的id
function clickingText(obj){
    //这个是抽象方法。当点击树节点文字时的动作
    //TODO;
    //alert(obj.id);
}

function resetValue(obj){
    resetValueForContainer(obj, valueContainerId);
}

//当点击checkbox后，为表单元素重新付值 obj :checkbox , valueContainerId : 存放checkbox值的input控件的ID
function resetValueForContainer(obj, valueContainerId){
    selectAllSon(obj);
    var c = document.getElementsByName(ystreecheckboxname);
    var tmp = "";
    for (var i = 0; i < c.length; i++) {
        if (c[i].checked) {
            tmp += c[i].value + ",";
        }
    }
    tmp = tmp.substring(0, tmp.length - 1);
    if (document.getElementById(valueContainerId)) 
        document.getElementById(valueContainerId).value = tmp;
    else 
        if (document.getElementsByName(valueContainerId)) {
            document.getElementsByName(valueContainerId)[0].value = tmp;
        }
}

function displayNode(obj){
    //改变节点 展开/关闭 的状态
    
    var pex = new Array();
    pex[0] = "expanded";//展开的
    pex[1] = "collapsed";//关闭的
    var displaySwitch;
    var path = obj.src;
    var img = path.substring(path.lastIndexOf("/") + 1);
    if (img.indexOf(pex[0]) != -1) {//已展开的
        img = img.replace(pex[0], pex[1]);
        displaySwitch = "none";
    }
    else {//已关闭的
        img = img.replace(pex[1], pex[0]);
        displaySwitch = "block";
    }
    obj.src = path.substring(0, path.lastIndexOf("/") + 1) + img;
    //显示/隐藏 子孙节点
    var imgid = obj.id;
    var parentdivid = imgid.replace("ystreeNodeImg_", "ystreeDivPane_");
    document.getElementById(parentdivid).style.display = displaySwitch;
}

function colorBack(obj){
    hiddenBar(obj);
    obj.style.color = "#000000";
}

function colorChange(obj){
    showBar(obj);
    obj.style.color = "#ff0000";
}

function selectAllSon(obj){
    var checkedid = obj.id;
    var c = document.getElementsByName(ystreecheckboxname);
    for (var i = c.length - 1; i >= 0; i--) {
        if (c[i].id == checkedid) {
            //说明该节点和被点节点是同级
        
        }
        else 
            if (c[i].id.indexOf(checkedid) == 0) {
                //说明该节点是被点节点的子孙节点
                c[i].checked = obj.checked;
            }
            else 
                if (checkedid.indexOf(c[i].id) == 0) {
                    //说明该节点是被点节点的祖先节点
                    //alert(c[i].id);
                    if (isMySonAllSelected(c[i].id)) {
                        c[i].checked = true;
                    }
                    else {
                        c[i].checked = false;
                    }
                }
    }
    
}

function isMySonAllSelected(currID){
    var f = true;
    var c = document.getElementsByName(ystreecheckboxname);
    for (var i = 0; i < c.length; i++) {
        if (c[i].id != currID && c[i].id.indexOf(currID) == 0) {
            if (c[i].checked == false) {
                f = false;
                break;
            }
        }
    }
    return f;
}

//引入文件
function $import(file){
    var ext = file.substring(file.lastIndexOf(".") + 1).toLowerCase();
    if (ext == "js") {
        var fileElem = document.createElement('script');
        fileElem.setAttribute('src', file);
        fileElem.setAttribute('type', 'text/javascript');
        document.getElementsByTagName('head')[0].appendChild(fileElem);
    }
    else 
        if (ext == "css") {
	        var fileElem = document.createElement('link');
	        fileElem.setAttribute('href', file);
	        fileElem.setAttribute('rel', 'stylesheet');
	        fileElem.setAttribute('type', 'text/css');
	        document.getElementsByTagName('head')[0].appendChild(fileElem);
        }
}

// import with a random query parameter to avoid caching
function $importNoCache(src){
    var ms = new Date().getTime().toString();
    var seed = "?" + ms;
    $import(src + seed);
}

